package cn.buk.qms.web;

import cn.buk.common.JsonResult;
import cn.buk.common.dto.CommonDto;
import cn.buk.tms.entity.misc.OperationLog;
import cn.buk.tms.car.dto.CarOrderDto;
import cn.buk.tms.car.dto.CarOrderRequestDto;
import cn.buk.tms.car.sc.SearchCarOrderCriteria;
import cn.buk.tms.car.service.RentalCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 租车服务，调用接口
 * @author yfdai
 */
@RestController
@RequestMapping("/rental")
public class RentalCarController extends BaseController {

  private final RentalCarService rentalCarService;

  @Autowired
  public RentalCarController(RentalCarService rentalCarService) {
    this.rentalCarService = rentalCarService;
  }

  /**
   * 新建租车订单
   * @param request 请求
   * @param dto
   * @return
   */
  @PostMapping(value = "/car/order", consumes = "application/json")
  public JsonResult createRentalCarOrder(HttpServletRequest request,
                                         @RequestBody CarOrderRequestDto dto
  ) {
    return rentalCarService.createRentalCarOrder(getEnterpriseId(request), getUsername(request), dto);
  }


  /**
   * 查找订单
   */
  @RequestMapping("/car/orders")
  public CommonDto<CarOrderDto> searchRentalCarOrders(HttpServletRequest request,
                                                      @RequestParam(value = "pageNo", defaultValue = "1") final int pageNo,
                                                      @RequestParam(value = "pageSize", defaultValue = "20") final int pageSize,
                                                      @RequestParam(value = "orderNo", required = false) String orderNo,
                                                      @RequestParam(value = "status", defaultValue = "-1") int status,
                                                      @RequestParam(value = "mobile", required = false) String mobile,
                                                      @RequestParam(value = "linkman", required = false) String linkman,
                                                      @RequestParam(value = "useDate", required = false) Date useDate
  ) {
    final int enterpriseId = getEnterpriseId(request);

    SearchCarOrderCriteria sc = new SearchCarOrderCriteria();
    sc.setPageNo(pageNo);
    sc.setPageSize(pageSize);
    sc.setOrderNo(orderNo);
    sc.setStatus(status);
    sc.setMobile(mobile);
    sc.setName(linkman);
    sc.setDdate(useDate);

    List<CarOrderDto> datas = rentalCarService.searchRentalCarOrders(enterpriseId, sc);

    CommonDto<CarOrderDto> commonDto = new CommonDto<>();
    commonDto.setDataList(datas);
    commonDto.setPage(sc.getPage());

    return commonDto;
  }

  /**
   * 查找订单详情
   */
  @RequestMapping("/car/orders/{id}")
  public CarOrderDto searchRentalCarOrder(HttpServletRequest request,
                                          @PathVariable("id") int orderId
  ) {
    final int enterpriseId = getEnterpriseId(request);

    return rentalCarService.searchRentalCarOrder(enterpriseId, orderId);
  }

  /**
   * 取消订单
   */
  @RequestMapping("/car/orders/{id}/cancel")
  public JsonResult cancelRentalCarOrder(HttpServletRequest request,
                                         @PathVariable("id") int orderId) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return rentalCarService.cancelRentalCarOrder(enterpriseId, username, orderId);
  }

  /**
   * 获取订单操作记录
   */
  @RequestMapping("/car/order/{orderId}/history")
  public List<OperationLog> searchRentalCarOrderHistory(HttpServletRequest request,
                                                     @PathVariable("orderId") int orderId
  ) {
    final int enterpriseId = getEnterpriseId(request);

    return rentalCarService.searchRentalCarOrderHistory(enterpriseId, orderId);
  }

  /**
   * 修改订单的价格
   */
  @PostMapping(value = "/car/order/{orderId}/price")
  public JsonResult updateRentalCarOrderPrice(HttpServletRequest request,
                                        @PathVariable("orderId") int orderId,
                                        @RequestParam("price") double price,
                                        @RequestParam("discount") double discount,
                                        @RequestParam("cost") double cost
  ) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return rentalCarService.updateRentalOrderPrice(enterpriseId, username, orderId, price, discount, cost);
  }

  /**
   * 修改租车订单的供应商
   * @param request
   * @param orderId
   * @param supplierId
   * @return
   */
  @PostMapping(value = "/car/order/{orderId}/supplier")
  public JsonResult updateRentalCarOrderSupplier(HttpServletRequest request,
                                             @PathVariable("orderId") final int orderId,
                                             @RequestParam("supplierId") int supplierId,
                                                 @RequestParam(value = "paymentMethodId", defaultValue = "0") int paymentMethodId) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return rentalCarService.updateRentalCarOrderSupplier(enterpriseId, username, orderId, supplierId, paymentMethodId);
  }

  /**
   * 修改租车订单的对外支付方式
   * @param request
   * @param orderId
   * @param paymentMethodId
   * @return
   */
  @PostMapping(value = "/car/order/{orderId}/paymentMethod")
  public JsonResult updateRentalCarOrderPaymentMethod(HttpServletRequest request,
                                                  @PathVariable("orderId") final int orderId,
                                                  @RequestParam("paymentMethodId") int paymentMethodId) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return rentalCarService.updateRentalCarOrderPaymentMethod(enterpriseId, username, orderId, paymentMethodId);
  }

  /**
   * 修改租车订单的 备注或联系人或联系电话
   * @param request
   * @param orderId
   * @param contentType 修改的类型
   *                    1-联系人
   *                    2-联系电话
   *                    3-备注
   * @param content 修改的内容（新内容）
   * @return
   */
  @PostMapping(value = "/car/order/{orderId}")
  public JsonResult updateRentalCarOrder(HttpServletRequest request,
                                                      @PathVariable("orderId") final int orderId,
                                                      @RequestParam("contentType") int contentType,
                                                      @RequestParam("content") String content) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return rentalCarService.updateRentalCarOrder(enterpriseId, username, orderId, contentType, content);
  }

  /**
   * 开始处理租车订单
   * @param request
   * @param orderId
   * @return
   */
  @PostMapping(value = "/car/order/{orderId}/status/processing")
  public JsonResult processRentalCarOrder(HttpServletRequest request,
                                      @PathVariable("orderId") final int orderId) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return rentalCarService.processRentalCarOrder(enterpriseId, username, orderId);
  }

  /**
   * 租车订单处理完毕
   * @param request
   * @param orderId
   * @param remark
   * @return
   */
  @PostMapping(value = "/car/order/{orderId}/status/finished")
  public JsonResult finishRentalCarOrder(HttpServletRequest request,
                                     @PathVariable("orderId") final int orderId,
                                     @RequestParam("remark") String remark) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return rentalCarService.finishRentalOrder(enterpriseId, username, orderId, remark);
  }

}
